/*
 * Bomberman.java
 *
 * Created on March 20, 2008, 8:42 PM
 */
package applet;

import bomberman.img.ImageDB;
import bomberman.*;
import java.awt.CardLayout;
import java.awt.Container;
import java.rmi.Naming;
import java.util.Hashtable;
import server.Server;

/**
 *
 * @author  eirikb
 */
public class Bomberman extends javax.swing.JApplet {

    private ImageDB idb;
    private Server server;
    private User user;
    private int actualFPS;
    private Hashtable<String, Game> games;
    private final int FPS = 50;  // not really FPS, ms to sleep...
    private final int W = 640;
    private final int H = 480;

    /** Initializes the applet Bomberman */
    @Override
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
            setSize(W, H + 100);

            games = new Hashtable<String, Game>();
            chatPane.setVisible(false);
            chatPane.setEnabledAt(1, false);
            chatPane.setEnabledAt(2, false);

            if (connect()) {
                infoLabel.setText("Connected!  Loading image database...");
                actualFPS = FPS;

                idb = server.getImageDB();
                idb.generateImages();
                infoLabel.setVisible(false);

                nicknameLabel.setEnabled(true);
                nicknameField.setEnabled(true);
                nicknameButton.setEnabled(true);
                new Thread() {

                    @Override
                    public void run() {
                        while (welcomePanel.isVisible() && welcomePanel.getGraphics() != null) {
                            welcomePanel.getGraphics().drawImage(idb.getWelcome().getImage(), 0, 0, W, H, null);
                        }
                    }
                }.start();
            } else {
                infoLabel.setText("Unable to establish a connection!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void publicChat(String message) {
        publicChatArea.append("\n" + message);
        publicChatArea.setCaretPosition(publicChatArea.getDocument().getLength());
    }

    public void updateProfile() {
        try {
            profileNameLabel.setText("Nick: " + user.getNick());
        } catch (Exception e) {
        }
        profileCharacterLabel.setIcon(idb.getCharacter().get(user.getCharacter()).getDown().get(0));
        profileCharacterComboBox.removeAll();
        for (String s : idb.getCharacter().keySet()) {
            profileCharacterComboBox.addItem(s);
        }
    }

    public void updateGameList(Hashtable<String, Game> games) {
        this.games = games;
        gameList.setListData(games.values().toArray(new Game[games.values().size()]));
    }

    @Override
    public void stop() {
        try {
            server.disconnect(user.getNick());
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.stop();
    }

    public void show(Container c) {
        CardLayout cl = (CardLayout) mainPanel.getLayout();
        cl.show(mainPanel, c.getName());
    }

    public void addGame(Game game) {
        games.put(game.getName(), game);
        gameList.removeAll();
        gameList.setListData(games.values().toArray(new Game[games.values().size()]));
    }

    public void removeGame(Game game) {
        games.remove(game.getName());
        gameList.setListData(games.values().toArray(new Game[games.values().size()]));
    }

    public void updateGame(Game game) {
        user.setGame(game);
        updateGameWaitPlayerList();
    }

    public void updateGameWaitPlayerList() {
        gameWaitPlayerList.setListData(user.getGame().getUsers().values().toArray());
        gameWaitPlayerLabel.setText("Players [" + user.getGame().getUsers().size() + "/" + user.getGame().getMax() + "]:");
    }

    public void startGame() {
        show(gamePanel);
        new Run().start();
    }

    public boolean connect() {
        try {
            server = (Server) Naming.lookup("rmi://localhost/BOMBERMAN");
            server.connect();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private class Run extends Thread {

        @Override
        public void run() {
            try {
                while (true) {
                    gamePanel.getGraphics().drawImage(idb.getWelcome().getImage(), 0, 0, null);

                    long time = System.currentTimeMillis();
                    Thread.sleep(actualFPS);
                    time = System.currentTimeMillis() - time;
                    if (time > FPS) {
                        actualFPS--;
                    } else {
                        actualFPS++;
                    }
                }
            } catch (Exception e) {
            }
        }
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chatPane = new javax.swing.JTabbedPane();
        publicChatPanel = new javax.swing.JPanel();
        publicChatScrollPane = new javax.swing.JScrollPane();
        publicChatArea = new javax.swing.JTextArea();
        publicChatField = new javax.swing.JTextField();
        gameChatPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        gameChatArea = new javax.swing.JTextArea();
        gameChatField = new javax.swing.JTextField();
        privateChatPanel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        privateChatArea = new javax.swing.JTextArea();
        privateChatField = new javax.swing.JTextField();
        mainPanel = new javax.swing.JPanel();
        welcomePanel = new javax.swing.JPanel();
        nicknameLabel = new javax.swing.JLabel();
        nicknameField = new javax.swing.JTextField();
        nicknameButton = new javax.swing.JButton();
        infoLabel = new javax.swing.JLabel();
        welcomeErrorLabel = new javax.swing.JLabel();
        menuPane = new javax.swing.JTabbedPane();
        gameListPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        gameList = new javax.swing.JList();
        gameListJoinButton = new javax.swing.JButton();
        profilePanel = new javax.swing.JPanel();
        profileNameLabel = new javax.swing.JLabel();
        profileCharacterLabel = new javax.swing.JLabel();
        profileCharacterComboBox = new javax.swing.JComboBox();
        profileSaveButton = new javax.swing.JButton();
        createGamePanel = new javax.swing.JPanel();
        createGameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        createGameMaxPlayers = new javax.swing.JComboBox();
        createGameButton = new javax.swing.JButton();
        createGameErrorLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        createGameShowNicks = new javax.swing.JComboBox();
        gameWaitPanel = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        gameWaitPlayerList = new javax.swing.JList();
        gameWaitQuitButton = new javax.swing.JButton();
        gameWaitReadyButton = new javax.swing.JToggleButton();
        gameWaitPlayerLabel = new javax.swing.JLabel();
        gamePanel = new javax.swing.JPanel();

        setBackground(java.awt.Color.green);

        chatPane.setMaximumSize(new java.awt.Dimension(640, 100));

        publicChatPanel.setMinimumSize(new java.awt.Dimension(0, 100));

        publicChatScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        publicChatArea.setColumns(20);
        publicChatArea.setLineWrap(true);
        publicChatArea.setRows(5);
        publicChatArea.setWrapStyleWord(true);
        publicChatScrollPane.setViewportView(publicChatArea);

        publicChatField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                publicChatFieldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout publicChatPanelLayout = new org.jdesktop.layout.GroupLayout(publicChatPanel);
        publicChatPanel.setLayout(publicChatPanelLayout);
        publicChatPanelLayout.setHorizontalGroup(
            publicChatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(publicChatField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
            .add(publicChatScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
        );
        publicChatPanelLayout.setVerticalGroup(
            publicChatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, publicChatPanelLayout.createSequentialGroup()
                .add(publicChatScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(publicChatField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        chatPane.addTab("Public", publicChatPanel);

        gameChatPanel.setEnabled(false);
        gameChatPanel.setMinimumSize(new java.awt.Dimension(0, 100));

        gameChatArea.setColumns(20);
        gameChatArea.setRows(5);
        jScrollPane3.setViewportView(gameChatArea);

        org.jdesktop.layout.GroupLayout gameChatPanelLayout = new org.jdesktop.layout.GroupLayout(gameChatPanel);
        gameChatPanel.setLayout(gameChatPanelLayout);
        gameChatPanelLayout.setHorizontalGroup(
            gameChatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(gameChatField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
            .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
        );
        gameChatPanelLayout.setVerticalGroup(
            gameChatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, gameChatPanelLayout.createSequentialGroup()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(gameChatField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        chatPane.addTab("Game", gameChatPanel);

        privateChatPanel.setMinimumSize(new java.awt.Dimension(0, 100));

        privateChatArea.setColumns(20);
        privateChatArea.setRows(5);
        jScrollPane4.setViewportView(privateChatArea);

        org.jdesktop.layout.GroupLayout privateChatPanelLayout = new org.jdesktop.layout.GroupLayout(privateChatPanel);
        privateChatPanel.setLayout(privateChatPanelLayout);
        privateChatPanelLayout.setHorizontalGroup(
            privateChatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(privateChatField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
            .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
        );
        privateChatPanelLayout.setVerticalGroup(
            privateChatPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, privateChatPanelLayout.createSequentialGroup()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(privateChatField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        chatPane.addTab("Public", privateChatPanel);

        getContentPane().add(chatPane, java.awt.BorderLayout.SOUTH);

        mainPanel.setMinimumSize(new java.awt.Dimension(640, 480));
        mainPanel.setLayout(new java.awt.CardLayout());

        welcomePanel.setBackground(java.awt.Color.green);
        welcomePanel.setName("welcomePane"); // NOI18N

        nicknameLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        nicknameLabel.setText("Nickname:"); // NOI18N
        nicknameLabel.setEnabled(false);

        nicknameField.setEnabled(false);
        nicknameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nicknameFieldActionPerformed(evt);
            }
        });

        nicknameButton.setText("OK"); // NOI18N
        nicknameButton.setEnabled(false);
        nicknameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nicknameButtonActionPerformed(evt);
            }
        });

        infoLabel.setFont(new java.awt.Font("Dialog", 1, 18));
        infoLabel.setText("Connecting..."); // NOI18N

        welcomeErrorLabel.setFont(new java.awt.Font("Dialog", 1, 12));
        welcomeErrorLabel.setForeground(java.awt.Color.red);
        welcomeErrorLabel.setText(" "); // NOI18N

        org.jdesktop.layout.GroupLayout welcomePanelLayout = new org.jdesktop.layout.GroupLayout(welcomePanel);
        welcomePanel.setLayout(welcomePanelLayout);
        welcomePanelLayout.setHorizontalGroup(
            welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(welcomePanelLayout.createSequentialGroup()
                .add(welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(welcomePanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(infoLabel))
                    .add(welcomePanelLayout.createSequentialGroup()
                        .add(249, 249, 249)
                        .add(welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(nicknameLabel)
                            .add(welcomeErrorLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 324, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, nicknameField)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, nicknameButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)))))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        welcomePanelLayout.setVerticalGroup(
            welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(welcomePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(infoLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(nicknameLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(nicknameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(nicknameButton)
                .add(3, 3, 3)
                .add(welcomeErrorLabel)
                .add(36, 36, 36))
        );

        mainPanel.add(welcomePanel, "welcomePanel");

        menuPane.setName("menuPane"); // NOI18N
        menuPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                menuPaneStateChanged(evt);
            }
        });

        gameListPanel.setBackground(java.awt.Color.yellow);

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 24));
        jLabel1.setText("Gamelist"); // NOI18N

        gameList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(gameList);

        gameListJoinButton.setText("JOIN");
        gameListJoinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameListJoinButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout gameListPanelLayout = new org.jdesktop.layout.GroupLayout(gameListPanel);
        gameListPanel.setLayout(gameListPanelLayout);
        gameListPanelLayout.setHorizontalGroup(
            gameListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(gameListPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(gameListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(gameListPanelLayout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 344, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(gameListJoinButton))
                    .add(jLabel1))
                .addContainerGap(216, Short.MAX_VALUE))
        );
        gameListPanelLayout.setVerticalGroup(
            gameListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(gameListPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(gameListPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(gameListJoinButton)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
                .addContainerGap())
        );

        menuPane.addTab("Gamelist", gameListPanel);

        profilePanel.setBackground(java.awt.Color.orange);

        profileNameLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        profileNameLabel.setText("NAME"); // NOI18N

        profileCharacterLabel.setText(" "); // NOI18N

        profileSaveButton.setText("SAVE"); // NOI18N

        org.jdesktop.layout.GroupLayout profilePanelLayout = new org.jdesktop.layout.GroupLayout(profilePanel);
        profilePanel.setLayout(profilePanelLayout);
        profilePanelLayout.setHorizontalGroup(
            profilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(profilePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(profilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(profileNameLabel)
                    .add(profilePanelLayout.createSequentialGroup()
                        .add(profileCharacterLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(profilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(profileSaveButton)
                            .add(profileCharacterComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(547, Short.MAX_VALUE))
        );
        profilePanelLayout.setVerticalGroup(
            profilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(profilePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(profileNameLabel)
                .add(18, 18, 18)
                .add(profilePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(profileCharacterLabel)
                    .add(profileCharacterComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 99, Short.MAX_VALUE)
                .add(profileSaveButton)
                .addContainerGap())
        );

        menuPane.addTab("Profile", profilePanel);

        createGamePanel.setBackground(java.awt.Color.red);

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel2.setText("Game name:"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel3.setText("Max players:"); // NOI18N

        createGameMaxPlayers.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4" }));

        createGameButton.setText("CREATE!"); // NOI18N
        createGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createGameButtonActionPerformed(evt);
            }
        });

        createGameErrorLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        createGameErrorLabel.setText(" "); // NOI18N

        jLabel4.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel4.setText("Show nicks:"); // NOI18N

        createGameShowNicks.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes", "No" }));

        org.jdesktop.layout.GroupLayout createGamePanelLayout = new org.jdesktop.layout.GroupLayout(createGamePanel);
        createGamePanel.setLayout(createGamePanelLayout);
        createGamePanelLayout.setHorizontalGroup(
            createGamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(createGamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(createGamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(createGamePanelLayout.createSequentialGroup()
                        .add(createGamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(createGameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel2)
                            .add(createGamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, createGameMaxPlayers, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(createGamePanelLayout.createSequentialGroup()
                                .add(createGameButton)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(createGameErrorLabel)))
                        .addContainerGap(504, Short.MAX_VALUE))
                    .add(createGamePanelLayout.createSequentialGroup()
                        .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(539, 539, 539))
                    .add(createGamePanelLayout.createSequentialGroup()
                        .add(createGameShowNicks, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(576, Short.MAX_VALUE))))
        );
        createGamePanelLayout.setVerticalGroup(
            createGamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(createGamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(createGameTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(createGamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(createGamePanelLayout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(createGameMaxPlayers, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(createGameShowNicks, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(createGameButton))
                    .add(createGameErrorLabel))
                .addContainerGap())
        );

        menuPane.addTab("Create Game", createGamePanel);

        mainPanel.add(menuPane, "menuPane");

        gameWaitPanel.setBackground(java.awt.Color.cyan);
        gameWaitPanel.setName("gameWaitPanel"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel5.setText("Waiting for other playes...");

        gameWaitPlayerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        gameWaitPlayerList.setEnabled(false);
        jScrollPane1.setViewportView(gameWaitPlayerList);

        gameWaitQuitButton.setText("QUIT");
        gameWaitQuitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameWaitQuitButtonActionPerformed(evt);
            }
        });

        gameWaitReadyButton.setText("READY!");
        gameWaitReadyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameWaitReadyButtonActionPerformed(evt);
            }
        });

        gameWaitPlayerLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        gameWaitPlayerLabel.setText("Players [x/x]:");

        org.jdesktop.layout.GroupLayout gameWaitPanelLayout = new org.jdesktop.layout.GroupLayout(gameWaitPanel);
        gameWaitPanel.setLayout(gameWaitPanelLayout);
        gameWaitPanelLayout.setHorizontalGroup(
            gameWaitPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(gameWaitPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(gameWaitPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(gameWaitPanelLayout.createSequentialGroup()
                        .add(gameWaitPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(gameWaitPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(gameWaitQuitButton)
                            .add(gameWaitReadyButton))
                        .addContainerGap(284, Short.MAX_VALUE))
                    .add(gameWaitPanelLayout.createSequentialGroup()
                        .add(gameWaitPlayerLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE)
                        .add(369, 369, 369))))
        );
        gameWaitPanelLayout.setVerticalGroup(
            gameWaitPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(gameWaitPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel5)
                .add(18, 18, 18)
                .add(gameWaitPlayerLabel)
                .add(7, 7, 7)
                .add(gameWaitPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(gameWaitPanelLayout.createSequentialGroup()
                        .add(gameWaitReadyButton)
                        .add(6, 6, 6)
                        .add(gameWaitQuitButton))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 258, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(132, Short.MAX_VALUE))
        );

        mainPanel.add(gameWaitPanel, "gameWaitPanel");

        gamePanel.setBackground(java.awt.Color.green);
        gamePanel.setName("gamePanel"); // NOI18N

        org.jdesktop.layout.GroupLayout gamePanelLayout = new org.jdesktop.layout.GroupLayout(gamePanel);
        gamePanel.setLayout(gamePanelLayout);
        gamePanelLayout.setHorizontalGroup(
            gamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 0, Short.MAX_VALUE)
        );
        gamePanelLayout.setVerticalGroup(
            gamePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 0, Short.MAX_VALUE)
        );

        mainPanel.add(gamePanel, "gamePanel");

        getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    private void nicknameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nicknameButtonActionPerformed
        nicknameButton.setEnabled(false);
        nicknameField.setEnabled(false);
        if (nicknameField.getText().length() > 0) {
            try {
                user = new User(nicknameField.getText());
                if (server.registerUser(new BombermanClient(this), user)) {
                    show(menuPane);
                    chatPane.setVisible(true);
                } else {
                    nicknameField.setText("");
                    welcomeErrorLabel.setText("Nickname already in use.");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            welcomeErrorLabel.setText("Please enter a nickname.");
        }
        nicknameButton.setEnabled(true);
        nicknameField.setEnabled(true);
    }//GEN-LAST:event_nicknameButtonActionPerformed

    private void publicChatFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_publicChatFieldActionPerformed
        if (publicChatField.getText().length() > 0) {
            try {
                server.publicChat(user.getNick() + ": " + publicChatField.getText());
            } catch (Exception e) {
                e.printStackTrace();
            }
            publicChatField.setText("");
        }
    }//GEN-LAST:event_publicChatFieldActionPerformed

    private void nicknameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nicknameFieldActionPerformed
        nicknameButton.doClick();
    }//GEN-LAST:event_nicknameFieldActionPerformed

    private void menuPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_menuPaneStateChanged
        if (menuPane.getSelectedComponent().equals(profilePanel)) {
            updateProfile();
        } else if (menuPane.getSelectedComponent().equals(createGamePanel)) {
            createGameTextField.setText("");
        }
    }//GEN-LAST:event_menuPaneStateChanged

    private void createGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createGameButtonActionPerformed
        if (createGameTextField.getText().length() > 0) {
            try {
                Game game = new Game(createGameTextField.getText(),
                        Integer.parseInt((String) createGameMaxPlayers.getSelectedItem()),
                        Boolean.valueOf((String) createGameShowNicks.getSelectedItem()),
                        16,
                        16);
                if (server.createGame(user.getNick(), game)) {
                    game.addUser(user);
                    user.setGame(game);
                    gameWaitReadyButton.setSelected(false);
                    gameWaitReadyButton.setEnabled(true);
                    updateGameWaitPlayerList();
                    show(gameWaitPanel);
                    chatPane.setEnabledAt(1, true);
                } else {
                    createGameErrorLabel.setText("Error: Game already exists");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            createGameErrorLabel.setText("Error: Please enter a name for the game");
        }
    }//GEN-LAST:event_createGameButtonActionPerformed

    private void gameListJoinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gameListJoinButtonActionPerformed
        if (gameList.getSelectedIndex() >= 0) {
            try {
                Game game = (Game) gameList.getSelectedValue();
                if (server.joinGame(user.getNick(), game.getName())) {
                    user.setGame(game);
                    game.addUser(user);
                    gameWaitReadyButton.setSelected(false);
                    show(gameWaitPanel);
                    chatPane.setEnabledAt(1, true);
                } else {
                    user.setGame(null);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }//GEN-LAST:event_gameListJoinButtonActionPerformed

    private void gameWaitReadyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gameWaitReadyButtonActionPerformed
        try {
            server.setReady(user.getNick(), gameWaitReadyButton.isSelected());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_gameWaitReadyButtonActionPerformed

    private void gameWaitQuitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gameWaitQuitButtonActionPerformed
        try {
            server.leaveGame(user.getNick());
        } catch (Exception e) {
            e.printStackTrace();
        }
        user.setGame(null);
        menuPane.setSelectedComponent(gameListPanel);
        show(menuPane);
        chatPane.setEnabledAt(1, false);
    }//GEN-LAST:event_gameWaitQuitButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane chatPane;
    private javax.swing.JButton createGameButton;
    private javax.swing.JLabel createGameErrorLabel;
    private javax.swing.JComboBox createGameMaxPlayers;
    private javax.swing.JPanel createGamePanel;
    private javax.swing.JComboBox createGameShowNicks;
    private javax.swing.JTextField createGameTextField;
    private javax.swing.JTextArea gameChatArea;
    private javax.swing.JTextField gameChatField;
    private javax.swing.JPanel gameChatPanel;
    private javax.swing.JList gameList;
    private javax.swing.JButton gameListJoinButton;
    private javax.swing.JPanel gameListPanel;
    private javax.swing.JPanel gamePanel;
    private javax.swing.JPanel gameWaitPanel;
    private javax.swing.JLabel gameWaitPlayerLabel;
    private javax.swing.JList gameWaitPlayerList;
    private javax.swing.JButton gameWaitQuitButton;
    private javax.swing.JToggleButton gameWaitReadyButton;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTabbedPane menuPane;
    private javax.swing.JButton nicknameButton;
    private javax.swing.JTextField nicknameField;
    private javax.swing.JLabel nicknameLabel;
    private javax.swing.JTextArea privateChatArea;
    private javax.swing.JTextField privateChatField;
    private javax.swing.JPanel privateChatPanel;
    private javax.swing.JComboBox profileCharacterComboBox;
    private javax.swing.JLabel profileCharacterLabel;
    private javax.swing.JLabel profileNameLabel;
    private javax.swing.JPanel profilePanel;
    private javax.swing.JButton profileSaveButton;
    private javax.swing.JTextArea publicChatArea;
    private javax.swing.JTextField publicChatField;
    private javax.swing.JPanel publicChatPanel;
    private javax.swing.JScrollPane publicChatScrollPane;
    private javax.swing.JLabel welcomeErrorLabel;
    private javax.swing.JPanel welcomePanel;
    // End of variables declaration//GEN-END:variables
}
